<?php
declare(strict_types=1);

namespace Modules\Shop\Service;

use HiCommon\Repository\RepairstoreRepository;
use HiCommon\Repository\RepairstoreGoodsRepository;
use HiCommon\Repository\ShopUserRepository;
use HiCommon\Repository\ShopImgRepository;
use HiCommon\Repository\ShopRepository;
use HiCommon\Repository\CheckpointTypeRepository;
use HiCommon\Repository\CheckpointServiceTypeRepository;

class AuthorService
{
    /**
     * 维修店详情
     */
    public function getShopCheckDetail(array $where)
    {
        $arr_option['with'] = ['service_type', 'area_name'];
        $detail = ShopRepository::get_one_by_where($where, $arr_option);

        $type_option['field'] = ['id', 'title'];
        $type_option['where'] = ['status' => 1];
        $type = CheckpointTypeRepository::get_all($type_option)->toarray();

        if ($detail) {
            $info = $detail->toarray();
            if (empty($info['longitude']) || empty($info['latitude'])) {
                $info['longitude'] = number_format($info['area_name']['Lng'],6);
                $info['latitude'] = number_format($info['area_name']['Lat'],6);
            }

            return ['detail' => $info, 'type' => $type];
        }
        return ['detail' => [], 'type' => $type];
    }

    /***
     * 检测机构管理员编辑
     */
    public function checkUpdate(array $data)
    {
        try {
            if ($data['cate'] != 3) {
                $service = $data['service'];//服务类型
                unset($data['service']);
                $data['cate'] = 1;
            }
            //修改管理员手机号，和名称
            ShopRepository::edit_one(array_filter($data));
            if ($data['cate'] != 3) {
                //删除原有服务类型
                CheckpointServiceTypeRepository::delete_where(['checkpoint_id' => $data['id']]);
                $service_ary = [];

                foreach ($service as $item) {
                    $service_ary[] = ['checkpoint_id' => $data['id'], 'title' => $item['title']];
                }
                //添加服务类型
                CheckpointServiceTypeRepository::add_many($service_ary);
            }
            $arr_data = [
                'shop_id' => $data['id'],
                'phone' => $data['phone'],
                'username' => $data['name'],
            ];
            ShopUserRepository::edit_one($arr_data);

            return hello_success('编辑成功');
        } catch (Exception $e) {
            return hello_error('编辑失败');
        }
    }

    /**
     * 维修店详情
     */
    public function getShopDetail(array $where)
    {
        $arr_field['with'] = ['shop_img', 'area_name'];
        $detail = ShopRepository::get_one_by_where($where, $arr_field);

        if ($detail) {
            $info = $detail->toarray();
            if (empty($info['longitude']) || empty($info['latitude'])) {
                $info['longitude'] = number_format($info['area_name']['Lng'],6);
                $info['latitude'] = number_format($info['area_name']['Lat'],6);
            }

            return ['detail' => $info];
        }
        return ['detail' => []];
    }

    /***
     * 维修店管理员编辑
     */
    public function repairUpdate(array $data)
    {
        try {
            $data['cate'] = 2;
            ShopRepository::edit_one(array_filter($data));
            //删除原有轮播图
            ShopImgRepository::delete_where_list(['shop_id' => $data['id']]);
            $img_ary = [];

            foreach ($data['img'] as $item) {
                $img_ary[] = ['shop_id' => $data['id'], 'img' => $item];
            }
            //添加轮播图
            ShopImgRepository::add_many($img_ary);

            if ($data['id'] && $data['longitude'] && $data['latitude']) {
                //更新门店对应的商品经纬度
                $map = ['repairstore_id' => $data['id']];
                $option = ['longitude' => $data['longitude'], 'latitude' => $data['latitude']];
                RepairstoreGoodsRepository::edit_one_by_where((array)$map, (array)$option);
            }

            //修改管理员手机号，和名称
            /*$arr_data = [
                'shop_id' => $data['id'],
                'phone' => $data['phone'],
                'username' => $data['name'],
            ];

            ShopUserRepository::edit_one($arr_data);*/

            return hello_success('编辑成功');
        } catch (Exception $e) {

            return hello_error('编辑失败');
        }
    }


}